﻿@using Telerik.Web.Mvc.UI;
@model OrderModel.AddOrderProductModel
@{
    var gridPageSize = EngineContext.Current.Resolve<SmartStore.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    
    ViewBag.Title = string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId);
}
<div class="section-header">
    <div class="title">
        @string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId) @Html.ActionLink("(" + T("Admin.Orders.Products.AddNew.BackToOrder") + ")", "Edit", new { id = Model.OrderId })
    </div>
    <div class="options">
    </div>
</div>

<div class="alert alert-info alert-dismissible">
	@T("Admin.Orders.Products.AddNew.Note1")
	<button type="button" class="close" data-dismiss="alert">&times;</button>
</div>

<div class="row mt-3 grid-filter">
	<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
		@Html.SmartLabelFor(model => model.SearchProductName)
		@Html.TextBoxFor(model => Model.SearchProductName, new { @class = "form-control" })
	</div>
	<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
		@Html.SmartLabelFor(model => model.SearchCategoryId)
		@Html.DropDownList("SearchCategoryId", Model.AvailableCategories, T("Admin.Common.All"))
	</div>
	<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
		@Html.SmartLabelFor(model => model.SearchManufacturerId)
		@Html.DropDownList("SearchManufacturerId", Model.AvailableManufacturers, T("Admin.Common.All"))
	</div>
	<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
		@Html.SmartLabelFor(model => model.SearchProductTypeId)
		@Html.DropDownList("SearchProductTypeId", Model.AvailableProductTypes, T("Admin.Common.All"))
	</div>

	<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
		<button type="submit" id="btnSearch" value="btnSearch" class="btn btn-secondary btn-block">
			<span>@T("Admin.Common.ApplyFilter")</span>
		</button>
	</div>
</div>


<div>
	@(Html.Telerik().Grid<OrderModel.AddOrderProductModel.ProductModel>()
			.Name("products-grid")
			.Columns(columns =>
			{
				columns.Bound(p => p.Id)
					.Width(80);
				columns.Bound(p => p.Name)
					.Template(p => @Html.LabeledProductName(0, p.Name, p.ProductTypeName, p.ProductTypeLabelHint))
					.ClientTemplate(@Html.LabeledProductName(null, "Name"));
				columns.Bound(p => p.Sku);
			})
			.Selectable()
			.Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
			.DataBinding(dataBinding => dataBinding.Ajax().Select("AddProductToOrder", "Order"))
			.ClientEvents(events => events.OnDataBinding("onDataBinding").OnRowSelect("onRowSelected"))
			.PreserveGridState()
			.EnableCustomBinding(true))
</div>

@Html.HiddenFor(model => Model.OrderId)

<script type="text/javascript">
    $(document).ready(function () {
		$('#btnSearch').click(function () {
            var grid = $('#products-grid').data('tGrid');
            grid.currentPage = 1; //new search. Set page size to 1
            grid.ajaxRequest();
            return false;
        });
    });

    $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
        if (event.keyCode == 13) {
			$("#btnSearch").click();
            return false;
        }
    });

    function onDataBinding(e) {
        var searchModel = {
            SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
            SearchCategoryId: $('#SearchCategoryId').val(),
            SearchManufacturerId: $('#SearchManufacturerId').val(),
            SearchProductTypeId: $('#SearchProductTypeId').val()
        };
        e.data = searchModel;
    }

    function onRowSelected(e) {
        productID = e.row.cells[0].innerHTML;
        //load "product details page" block
        var productDetailsActionUrl = '@Html.Raw(Url.Action("AddProductToOrderDetails", "Order", new { orderId = Model.OrderId, productId = "productidplaceholder" }))';
        productDetailsActionUrl = productDetailsActionUrl.replace("productidplaceholder", productID);
        setLocation(productDetailsActionUrl);
    }
</script>
